package com.daily.pump.dialog;

import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.daily.pump.R;
import com.daily.pump.adapter.UserListAdapter;
import com.daily.pump.model.SimpleWifiBean;
import com.daily.pump.utils.ScanResultListener;
import com.daily.pump.utils.WifiHelper;
import com.daily.pump.utils.WifiUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.xunxi.mimi.ui.base.BaseRecycleAdapter;
import com.xunxi.mimi.utils.LogUtil;
import com.xunxi.mimi.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Desc:我的-设置视频聊天价格
 * <p>
 * Author: [李豫]
 * Date: 2018-08-02
 * Copyright: Copyright (c) 2010-2018
 * Updater:
 * Update Time:
 * Update Comments:
 */
public class WifiConnDialog extends DialogFragment implements ScanResultListener {

    private UserListAdapter wifiListAdapter;

    private WifiHelper wifiHelper;

    private SmartRefreshLayout refreshLayout;

    private SimpleWifiBean mScanResult;

    private WiFiConnDialogListener mWiFiConnDialogListener;

    public void setWiFiConnDialogListener(WiFiConnDialogListener listener) {
        this.mWiFiConnDialogListener = listener;
    }

    public static WifiConnDialog newInstance() {
        WifiConnDialog commentDeleteDialog = new WifiConnDialog();
        return commentDeleteDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(true);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.customDialog);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().setCanceledOnTouchOutside(true);
        Window window = getDialog().getWindow();
        window.setGravity(Gravity.CENTER);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View dialogView = inflater.inflate(R.layout.dialog_wifi_conn, container, false);
        initData(dialogView);
        return dialogView;
    }

    private void initData(View dialogView) {
        refreshLayout = dialogView.findViewById(R.id.refresh_layout);
        RecyclerView recyclerView = dialogView.findViewById(R.id.recyclerview);

        refreshLayout.setEnableRefresh(true);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                startScan();
            }
        });
        wifiListAdapter = new UserListAdapter(getActivity());
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(wifiListAdapter);
        wifiListAdapter.setOnItemClickListener(new BaseRecycleAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {
//                mScanResult = wifiListAdapter.setSelect(position);
            }
        });
        dialogView.findViewById(R.id.login_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mScanResult != null) {
                    boolean connectTargetWifi = WifiUtil.connectTargetWifi(getActivity(), mScanResult);
                    if (connectTargetWifi) {
                        if (mWiFiConnDialogListener != null) {
                            mWiFiConnDialogListener.onConnResult(mScanResult);
                        }
//                        DaoSharedPreferences.getInstance().setWifiConfig(mScanResult);
                        dismissAllowingStateLoss();
                    } else {
                        ToastUtil.show(getActivity(), "Connection failed");
                    }
                    LogUtil.error(WifiConnDialog.class, "connectTargetWifi:" + connectTargetWifi);
                } else {
                    ToastUtil.show(getActivity(), "Please select the wifi first.");
                }
            }
        });
        refreshLayout.autoRefresh();
    }

    private void startScan() {
        if (wifiHelper == null) {
            wifiHelper = new WifiHelper(this, this);
        }
        wifiHelper.startScan();
    }

    public void show(FragmentManager manager) {
        if (!isShowing()) {
            show(manager, WifiConnDialog.class.getSimpleName());
        }
    }

    public boolean isShowing() {
        if (getDialog() != null && getDialog().isShowing()) {
            return true;
        }
        return false;
    }

    @Override
    public void resultSuc(List<ScanResult> list, boolean isLastTime) {
//        wifiListAdapter.setDataSource(filterWifi(list));

        refreshLayout.finishRefresh();
    }

    private List<SimpleWifiBean> filterWifi(List<ScanResult> list) {
        List<SimpleWifiBean> wifiResults = new ArrayList<>();
        for (ScanResult scanResult : list) {
            if (!TextUtils.isEmpty(scanResult.SSID)) {
                SimpleWifiBean simpleWifiBean = new SimpleWifiBean();
                simpleWifiBean.wifiName = scanResult.SSID;
                simpleWifiBean.capabilities = scanResult.capabilities;
                simpleWifiBean.level = WifiUtil.getLevel(scanResult.level) + "";
                wifiResults.add(simpleWifiBean);
            }
        }
        return wifiResults;
    }

    @Override
    public void filterFailure() {

    }

    @Override
    public void connectedWifiCallback(WifiInfo info) {

    }

    public interface WiFiConnDialogListener {
        void onConnResult(SimpleWifiBean scanResult);
    }
}

